/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package presentation.proportional;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;

/**
 *
 * @author emilio
 * questa layout manager è indicato per settare un unico elemento
 * con un margine relativo al pannello padre
 *
 */
public class relativeMarginLayout implements LayoutManager {

    private Float  percentTop=new Float(0);
    private Float  percentRight=new Float(0);
    private Float  percentBottom=new Float(0);
    private Float  percentLeft=new Float(0);

    public relativeMarginLayout(Float percentTop, Float percentRight,
                                Float percentBottom, Float percentLeft){
        this.percentTop=(percentTop >= 0)? percentTop : 0;
        this.percentRight=(percentRight >=0) ? percentRight:0;
        this.percentLeft=(percentLeft>=0)? percentLeft:0;
        this.percentBottom=(percentBottom>=0)?percentBottom:0;
    }

    public void addLayoutComponent(String name, Component comp) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void removeLayoutComponent(Component comp) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Dimension preferredLayoutSize(Container parent) {
        return new Dimension(0,0);
    }

    public Dimension minimumLayoutSize(Container parent) {
        return new Dimension(0,0);
    }

    public void layoutContainer(Container parent) {
        int nCount= parent.getComponentCount();
        if(nCount>1)
          throw new UnsupportedOperationException("Too much component in container");
        Component c=parent.getComponent(0);
        Insets insets = parent.getInsets();
        int maxWidth = parent.getWidth()-(insets.left + insets.right);
        int maxHeight = parent.getHeight()-(insets.top + insets.bottom);
        Float left= new Float(this.percentLeft * maxWidth/100);
        Float top = new Float(this.percentTop * maxHeight/100);
        Float right=new Float(this.percentRight * maxWidth/100);
        Float bottom = new Float(this.percentBottom * maxHeight/100);
        c.setBounds(insets.left+left.intValue(), insets.top+top.intValue(), (maxWidth-left.intValue()-right.intValue()), (maxHeight-top.intValue()-bottom.intValue()));
    }

}
